
package com.scalar.chiptrack.operations.dataentry.actions;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
//import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
//import java.util.StringTokenizer;
import java.util.HashMap;
//import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.scalar.chiptrack.ChipTrackAction;
//import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.dataentry.forms.InventoryDataEntryForm;
import com.scalar.chiptrack.operations.process.dao.LotDAO;
//import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.process.VendorLocation;
//import com.scalar.chiptrack.commons.Location;
import com.scalar.chiptrack.utils.StringUtils;
//import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.ProcessStep;
//import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.commons.SessionKeys;

/*  

 * Added by Vasanth April 26 2003
 * Creating new lots for the Finished goods

*/

public final class InventoryDataEntryAction extends ChipTrackAction {

    public InventoryDataEntryAction() {
    }

	public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {

		ArrayList partNumberList = new ArrayList();

		InventoryDataEntryForm inventoryDataEntryForm = (InventoryDataEntryForm) form;
		HttpSession ses = request.getSession(true);		
			
		String action =(String)request.getParameter("action");
				
			int prevProcessStepId=0;
			String prevLotId="";
		
		/*
		  * Returning the jsp with the information about the available partnumbers and locations for
		  * finished goods
		*/
		
		if(action.equals("partnumber") || action.equals("save")){
		
			partNumberList = ProductDao.getInventoryPartNumber("finished%");
			PartNumber partNumber = null;
			ArrayList partNumberSelectList = new ArrayList();

			for(int i=0;i<partNumberList.size();i++){
				partNumber = (PartNumber)partNumberList.get(i);
				partNumberSelectList.add(new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(), partNumber.getProcessStepId()));
			}
				request.setAttribute("partNumberSelectList",partNumberSelectList);
				ArrayList inventoryLocationList= VendorDAO.getFinishedGoodsLocations();
				ArrayList vendorLocationList = new ArrayList();
				VendorLocation vendorLocation = null;
			
								
					if(inventoryLocationList == null)
						inventoryLocationList = new ArrayList(0);
			
						for(int i=0;i<inventoryLocationList.size();i++){
							vendorLocation=(VendorLocation)inventoryLocationList.get(i);
							vendorLocation.setLocationID(vendorLocation.getLocationID());
							vendorLocation.setLocationName(vendorLocation.getLocationName());
							vendorLocationList.add(new org.apache.struts.util.LabelValueBean(vendorLocation.getLocationName(),new Integer(vendorLocation.getLocationID()).toString()));				
						}

						request.setAttribute("locationSelectList",vendorLocationList);
						request.setAttribute("DateIn", StringUtils.formatDateToString(new Date(System.currentTimeMillis()), "MM/dd/yyyy"));	
		 
		}
		
		
		/* 
		 * saving the lot number
		 * If the Lot Number is already available then the information is updated else, created
		*/

		if(action.equals("save")){
					
					
					HashMap errors = new HashMap();
					HashMap info = new HashMap();
	                request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
		            request.setAttribute (SessionKeys.INFO_MESSAGES, info);

					String LotString = inventoryDataEntryForm.getLotNo();
					String processStepId	= inventoryDataEntryForm.getProcessStepId();
					String locationId		= inventoryDataEntryForm.getLocationId();
					String dateIn			= inventoryDataEntryForm.getDateIn();
					String dateOut			= inventoryDataEntryForm.getDateOut();
					String quantityIn		= inventoryDataEntryForm.getQuantityIn();
					String quantityOut		= inventoryDataEntryForm.getQuantityOut();
					String newLotNo			= inventoryDataEntryForm.getNewLotNo();
					String dateCode			= inventoryDataEntryForm.getDateCode();
					String exists			= request.getParameter("Exists");

									
						
						if(dateOut.equals(""))
							dateOut = null;

						if(quantityOut.equals(""))
							quantityOut = null;	
						//Bhaskar commented this block on 18 Dec 2003
						/*if (dateCode.equals("")){
							quantityOut = null;
						}*/
							
							String type = ProcessStep.getProcessType(Integer.parseInt(processStepId));

							
							request.setAttribute("inventorydataentryform",inventoryDataEntryForm);
										
							boolean success = false;
											
								//Step1 : Save Inventory Lot 
								// check for the values of product and location
								// check before inserting the lot number into the DB
								if((exists != null) && ((exists.equalsIgnoreCase("true") || !(LotDAO.checkWhetherLotExists(newLotNo, processStepId, locationId))))){
									try{
										/*This method is called by Bhaskar on 18 Dec 2003 inorder to 
										 *get the lotId for the specific proc_step_id, loc_id and lot_number
										 */
										String lotId = LotDAO.getLotId(newLotNo, locationId, processStepId);
										if (lotId.equals(""))
										{
											lotId = "-1";
										}

										/*This method is called by Bhaskar  18 December  2003. 
										 * To update the lot_current_status to close from running. This will be 
										 * called in all DataEntry classes just before actual create or updation 
										 * concept. Even if a lotId of -1 is passed still the procedure will execute
										 * successfully
										 */
										LotDAO.changeLotStatusToCloseByLotId(null, lotId.trim());

										

										LotDAO.createInventoryLotForFinishedGoods(null,processStepId,locationId,newLotNo,quantityIn,quantityOut,dateIn,dateOut,dateCode); 
										info.put(" Lot has been successfully created for "+newLotNo, null);
									}
									catch (Exception e) { info.put ("Problem in saving the lot details",""); }
								}
								else{
									info.put(newLotNo+" lot already exists. Do you want to over-write it?? ", null);
									request.setAttribute("Exists", "true");
									inventoryDataEntryForm.setDateIn(dateIn);
									inventoryDataEntryForm.setNewLotNo(newLotNo);
									inventoryDataEntryForm.setQuantityIn(quantityIn);
									inventoryDataEntryForm.setQuantityOut(quantityOut);
									inventoryDataEntryForm.setDateOut(dateOut);
									inventoryDataEntryForm.setDateCode(dateCode);
									request.setAttribute("inventorydataentryform",inventoryDataEntryForm);
									return (mapping.findForward("success"));
									
								}
								
									success	= true;
											
							
							
		}

		        request.setAttribute("DateIn", StringUtils.formatDateToString(new Date(System.currentTimeMillis()), "MM/dd/yyyy"));
				inventoryDataEntryForm.setNewLotNo("");
				inventoryDataEntryForm.setQuantityIn("");
				inventoryDataEntryForm.setQuantityOut("");
				inventoryDataEntryForm.setDateOut("");
				inventoryDataEntryForm.setDateCode("");
                request.setAttribute("inventorydataentryform",inventoryDataEntryForm);
		
			return (mapping.findForward("success"));
					
					
 	}
		
	
	
}